<?php
/**
 * Created by PhpStorm.
 * User: 丶晓
 * Date: 2017/8/24
 * Time: 16:58
 */

namespace app\home\common;


class WechatSDK extends WechatBase // 商户小程序
{
    private $appid;
    private $appsecrt;

    public function __construct($id, $secret)
    {
        $this->appid = $id;
        $this->appsecrt = $secret;
    }

    /**
     * 获取 access_tonken
     * @param string $token_file 用来存储token的临时文件
     */
    public function getAccessToken($token_file = './access_token')
    {
        // 考虑过期问题，将获取的access_token存储到某个文件中
        $life_time = 7200;
        if (file_exists($token_file) && time() - filemtime($token_file) < $life_time) {
            // 存在有效的access_token
            return file_get_contents($token_file);
        }
        // 目标URL：
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecrt;
        // 向该URL，发送GET请求
        $result = $this->_requestGet($url);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return isset($result_obj->access_token) ? $result_obj->access_token : null;
    }

    /**
     * 获取小程序用户访问人数统计
     *
     */
    public function getSum($token, $begin_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailysummarytrend?access_token=" . $token;
        $data = json_encode([
            "begin_date" => $begin_date,
            "end_date" => $begin_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
     * 日访问量
     */
    public function dailyVisit($token, $begin_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $begin_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
     * 周访问量
     */
    public function weeklyVisit($token, $begin_date, $end_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidweeklyvisittrend?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $end_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
    * 月访问量
    */
    public function monthlyVisit($token, $begin_date, $end_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyvisittrend?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $end_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
    * 访问分布
    */
    public function distribution($token, $date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitdistribution?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $date,
            'end_date' => $date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
     * 日访问留存
     */
    public function dailyRetain($token, $begin_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $begin_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
     * 周访留存
     */
    public function weeklyRetain($token, $begin_date, $end_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidweeklyretaininfo?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $end_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
    * 月留存
    */
    public function monthlyRetain($token, $begin_date, $end_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidmonthlyretaininfo?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $end_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
      * 访问页面
      */
    public function visitPage($token, $begin_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappidvisitpage?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $begin_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }

    /*
    * 用户画像
    */
    public function userPortrait($token, $begin_date, $end_date)
    {
        if (!$token) {
            return false;
        }
        $url = "https://api.weixin.qq.com/datacube/getweanalysisappiduserportrait?access_token=" . $token;
        $data = json_encode([
            'begin_date' => $begin_date,
            'end_date' => $end_date
        ], JSON_UNESCAPED_UNICODE);
        //向该URL，发送GET请求
        $result = $this->_requestPost($url, $data);
        if (!$result) {
            return false;
        }
        // 存在返回响应结果
        $result_obj = json_decode($result);
        return $result_obj ? $result_obj : null;
    }
}